﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.Entities;
using CorporateGym.DAL;

namespace BLL.Managers
{
    public class MalfunctionManager
    {
        private static MalfunctionManager m_This;
        private static readonly object sr_SyncObj = new Object();
        public static MalfunctionManager Instance
        {
            get
            {
                if (m_This == null)
                {
                    lock (sr_SyncObj)
                    {
                        if (m_This == null)
                        {
                            m_This = new MalfunctionManager();
                        }
                    }
                }

                return m_This;
            }
        }

        private MalfunctionManager()
        {
        }

        public int AddMalfunction(string i_Subject, string i_Description)
        {
            int newMalfunctionId = DataAccess.Instance.AddMalfunction(
                i_Subject,
                i_Description,
                DateTime.Now,
                GymLoginManager.Instance.LoggedinUser.WorkerId);
            //sendEmail();
            return newMalfunctionId; 
        }

        public bool CloseMalfunction(Malfunction i_Malfunction)
        {
            return DataAccess.Instance.CloseMalfunction(
                i_Malfunction.Id,
                GymLoginManager.Instance.LoggedinUser.WorkerId);
        }

        public List<Malfunction> GetAllMalfunctions(int i_Status)
        {
            List<Malfunction> malfunctions = new List<Malfunction>();

            malfunctions = DataAccess.Instance.GetAllMalfunctions(i_Status).
                ConvertAll((row) => Malfunction.CreateFromRow(row));

            return malfunctions;
        }
    }
}
